<?php

use app\config\Db;
use Core\Result\Result;
use Core\Common\Common;

/**
 * 店铺信息
 */

function shopInfo($data)
{
    $shop_id = $data['shop_id'];
    $where = " `shop_id` = $shop_id and `status` = 1";
    $field = 'shop_title,add_time,shop_logo,cust_addr,description,kefu_phone,kefu_weixin';
    $db = new Db();
    $shopInfo = $db->find("select $field from ysmz_shop where $where");
    $fileInfo = $db->find("select file_id,storage,`domain`,file_path from `ysmz_file` where `file_id` =$shopInfo[shop_logo]");
    $shopInfo['shop_logo'] = Common::fileUrl($fileInfo);
    Result::success($shopInfo);
}

/**
 * 店铺商品
 */
function shopGoodsList($data)
{
    $shop_id = $data['shop_id'];
    $page = $data['page'];
    $limit = $data['limit'];
    $type = $data['type'];
    $name = $data['name'];
    $where = " ys.`shop_id` = $shop_id and is_delete=0 and is_adopt=1 and is_top=1 and ys.status = 1";
    if ($name) $where .= " and `name` like '%$name%'";
    switch ($type) {
        case 1://综合
            $order = "order by sort desc,unreal_sales_volume desc,sales_volume desc,create_time desc";
            break;
        case 2://销量
            $order = "order by sales_volume desc,sort desc,create_time desc";
            break;
        case 3://新品
            $order = "order by create_time desc";
            break;
        case 4://价格
            $order = "order by sell_price asc,sales_volume desc,sort desc,create_time desc";
            break;
        default:
            $order = "order by sort desc,unreal_sales_volume desc,sales_volume desc,create_time desc";
            break;
    }
    $field = 'goods_id,ysg.category_id,ysg.shop_id,ysg.name,ysg.goods_type,ysg.title,old_price,sell_price,game_points,img_id,hits,sales_volume,unreal_sales_volume,create_time';
    $db = new Db();
    $goodsInfo = $db->query("select $field from ysmz_shop_goods as ysg inner join ysmz_shop as ys on ysg.shop_id = ys.shop_id where $where $order limit $page,$limit");
    $imgIds = implode(',', array_column($goodsInfo, 'img_id', 'img_id'));
    $categoryIds = implode(',', array_column($goodsInfo, 'category_id'));
    $fileInfo = [];
    if(!empty($imgIds)){
        $fileInfo = $db->query("select file_id,storage,`domain`,file_path from ysmz_file where file_id in($imgIds)");
    }
    foreach ($fileInfo as $k => $v) {
        $fileInfo[$k]['file_url'] = Common::fileUrl($v);
    }
    $fileInfo = array_column($fileInfo, 'file_url', 'file_id');
    $categoryInfo = [];
    if(!empty($categoryIds)){
        $categoryInfo = $db->query("select category_id,category_name from ysmz_shop_category where category_id in($categoryIds)");
    }
    $categoryInfo = array_column($categoryInfo, 'category_name', 'category_id');
    foreach ($goodsInfo as $k => $v) {
        $goodsInfo[$k]['img_url'] = $fileInfo[$v['img_id']] ?? '';
        $goodsInfo[$k]['category_name'] = $categoryInfo[$v['category_id']] ?? '(未分类)';
    }
    Result::success($goodsInfo);
}